//
//  PatientListViewController.swift
//  wechatManager
//
//  Created by tangchunhui on 15/4/15.
//  Copyright (c) 2015年 tangchunhui. All rights reserved.
//

import Foundation


class PatientListViewController :UITableViewController,UISearchBarDelegate{
    @IBOutlet var searchBar:UISearchBar?
    var list:Array<Dictionary<String,String>>=Array<Dictionary<String,String>>()
    
    var c_pati_id:String=""
    var c_sick_no:String=""
    var c_name:String=""
    var c_sex:String=""
    var c_age:String=""
    var md5str:String=""
    
    override func viewDidLoad() {
        self.title = "查找患者"
        
    }
    func execmodal(s:String,withhud:Bool){
        var modal = ModalPatientList.init(sick_no:s)

        DataModal.instance().executeModal(modal, withhud:withhud,completion:{(data:Dictionary!)->()  in
            self.list = data["data"] as! [Dictionary<String,String>]
            dispatch_async(dispatch_get_main_queue(), {
                self.tableView.reloadData()
            
            })
        },failure:nil)
    }
     override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return 1
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var row = indexPath.row
        var data:Dictionary<String,String> = self.list[row]
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as! UITableViewCell
        cell.textLabel?.text = data["name"]
        return cell
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var row = indexPath.row
        var data:Dictionary<String,String> = self.list[row]
        self.c_age = data["age"]!
        self.c_sick_no = data["sick_no"]!
        self.c_pati_id = data["pati_id"]!
        self.c_name = data["name"]!
        self.c_sex = data["sex"]!
        self.performSegueWithIdentifier("ScanQR", sender: self)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var vc:ScanQrController = segue.destinationViewController as! ScanQrController
        vc.pati_id = c_pati_id
        vc.sick_no = c_sick_no
        vc.pati_sex = c_sex
        vc.pati_name = c_name
        vc.age = c_age
        vc.md5str = md5str
    }
    
    //关于搜索
    
    func searchBarTextDidBeginEditing(searchBar:UISearchBar){
       searchBar.showsCancelButton = true
    }
    func searchBarSearchButtonClicked(searchBar:UISearchBar ){
       self.searchBar!.resignFirstResponder()
        var s :String = self.searchBar!.text
       self.execmodal(s, withhud: true)
        self.searchBar!.showsCancelButton = false
        
    }
    func searchBarCancelButtonClicked(searchBar:UISearchBar){
        self.searchBar!.resignFirstResponder()
        self.searchBar!.showsCancelButton = false
    }
    
    

}